package datastructure.stack;

import datastructure.array.Array;

public class MyArrayStack<E> implements MyStack<E> {
    private Array<E> arr;

    // 默认大小
    public MyArrayStack() {
        arr = new Array<E>();
    }

    public MyArrayStack(int capacity) {
        arr = new Array<E>(capacity);
    }

    @Override
    public void push(E e) {
        arr.addLast(e);

    }

    @Override
    public E pop() {
        return arr.removeLast();
    }

    @Override
    public E peek() {
        return arr.getLast();
    }

    @Override
    public int getSize() {
        return arr.getSize();

    }

    @Override
    public boolean isEmpty() {
        return arr.isEmpty();
    }

    // 获取容量
    public int getCapacity() {
        return arr.getCapacity();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Stack:[");
        for (int i = 0; i < arr.getSize(); i++) {
            sb.append(arr.get(i));
            if (i != arr.getSize() - 1) {
                sb.append(",");
            }
        }
        sb.append("]-->top");
        return sb.toString();
    }
}
